<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="../css/jsdeliverbootstrap.min.css">
<!--    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css"-->
<!--          integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">-->
    <link rel="stylesheet" href="../css/pet-store-plus.css">

    <link rel="stylesheet" href="/css/staticfilebootstrap.min.css">
<!--    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">-->
    <script src="/js/chart.js"></script>
<!--    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>-->
    <title>权限管理</title>

</head>
<body>

<div th:replace="seller_commom"></div>
<div class="content" id="content">
    <div style="grid-row: span 1;grid-column: span 5"></div>
    <button class="Button" id="invite" onclick="copyInviteCode(this)">
        复制邀请码
    </button>
    <script th:inline="javascript">

    </script>
    <div  class="modal" id="myModal2" style="display: none">
        <div class="modal-content">
            <div class="ic-row">
                <label for="inviteCode" id="newPwLb">邀请码</label>
                <input id="inviteCode" type="text" readonly>
            </div>
            <div class="button-grp">
                <button type="button" class="Button" id="copy">复制</button>
                <button type="button" onclick="closeModal()"class="Button" style="font-size: large;color: #222222">关闭</button>
            </div>

        </div>
    </div>
     <div class="tab-bg">
<!--         <table class="Administrators_tab" th:fragment="Administratorstab" id="Administratorstab">-->
             <table class="Administrators_tab"  id="Administratorstab" style="font-size: x-large">
             <caption></caption>
             <thead>
             <tr>
                 <th>id</th>
                 <th>操作</th>
             </tr>
             </thead>
             <tbody>
             <tr th:each="admin:${adminList}">
                 <td th:text="${admin.adminname}" class="adminname">管理员id</td>
<!--                 <td><button class="Button" style="display: inline-block;" onclick="deleteAdmin(this)" th:unless="${admin.adminname=='admin'}">删除</button></td>-->
                <td> <button class="Button" style="display: inline-block;"
                         onclick="deleteAdmin(this)"
                         th:text="${admin.adminname != 'admin' ? '删除' : '不可删除'}"
                         th:disabled="${admin.adminname == 'admin'}">不可删除
                 </button>

                 </td>
             </tr>
             </tbody>
         </table>
     </div>
</div>
<script th:inline="javascript">

    function deleteAdmin(button){
                 console.log('delete Admin');
                 var row=button.parentNode.parentNode;
        var url = '/admin/deleteAdmin';
        const adminId = row.querySelector('.adminname').innerText;
        console.log(adminId);

        var result = confirm("Are you sure you want to delete the admin "+adminId+"?");

        if (result) {
            fetch(url, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ adminId: adminId }),
            })
                .then(response => response.json())
                .then(data => {
                    console.log(data);
                    row.remove();
                    alert('删除管理员'+adminId+'成功');
                })
                .catch(error => {
                    console.error('Error:', error);
                });
        } else {
            // 如果用户点击取消，可以选择执行其他操作或不执行任何操作
            console.log("Action cancelled by user");
        }



    }
    function copyInviteCode(button){
        // 创建弹窗元素
        var modal=document.getElementById('myModal2');
        var url="/admin/getInviteCode";
        var invitecode=document.getElementById('inviteCode');
        modal.style.display="block";
        fetch(url,{
            method:'GET',
        }).then(response=>response.json())
            .then(data=>{
                console.log(data);
                invitecode.value=data.data;
            })
            .catch(error=>{
                console.error('请求发送失败:', error);
            })
    }
    function closeModal(){
        console.log('关闭弹窗');
        var modal=document.getElementById('myModal2');
        modal.style.display="none";
    }

    document.getElementById('copy').addEventListener('click',function () {

        var tempInput=document.createElement('input');
        tempInput.value=document.getElementById('inviteCode').value;
        document.body.appendChild(tempInput);
        tempInput.select();
        document.execCommand('copy');
        document.body.removeChild(tempInput);

        alert('已复制邀请码' + tempInput.value); // 只有确定选项，没有取消选项

// 延迟调用其他函数
        setTimeout(function() {
           closeModal(); // 替换 yourFunction() 为您希望调用的其他函数名
        }, 0);
    })
</script>
</body>
</html>